﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class admin_ymgl_bthzw : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        if (!IsPostBack)
        {
            foreach (string pt in PageTypes.types)
            {
                this.ddlSelectPage.Items.Add(pt);
            }
            showTitleContent();
        }
    }

    private void showTitleContent()
    {
        string type = this.ddlSelectPage.Text;
        if (string.IsNullOrEmpty(type))
        {
            return;
        }
        PageBT pbt = new PageBT();
        this.tbTitle.Text = pbt.GetTitle(type).Trim();
        this.tbContent.Text = pbt.GetContent(type);
    }


    protected void ddlSelectPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        showTitleContent();
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.ddlSelectPage.Text))
        {
            message_show("请先选择页面类型");
            return;
        }
        if (this.tbTitle.Text.Trim().Length > 40)
        {
            message_show("标题不能超过40个字符");
            return;
        }
        if (this.tbContent.Text.Trim().Length > 5000)
        {
            message_show("正文内容不能超过5000个字符");
            return;
        }        
        PageBT pbt = new PageBT();
        string type = this.ddlSelectPage.Text;
        string title = this.tbTitle.Text.Trim();
        string content = this.tbContent.Text;
        if (string.IsNullOrEmpty(content.Trim()))
        {
            content = content.Trim();
        }
        ContentValidate cv = new ContentValidate();
        title = cv.Process(title);
        content = cv.Process(content);
        int adminID = int.Parse(Session["admin_id"].ToString());
        if (pbt.CreateOrUpdatePage(type, title, content, adminID))
        {
            message_show("修改成功");
        }
        else
        {
            message_show("修改失败");
        }
    }

    /// <summary>
    /// 信息提示
    /// </summary>
    /// <param name="msg">提示信息</param>
    private void message_show(string msg)
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "信息提示", "<script language='javascript'>  warning('" + msg + "');</script>");
    }
}
